/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package interfaz;

import java.io.File;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileNameExtensionFilter;
import main_package.*;

/**
 *
 * @author Dario
 */
public class PlantillaExpertoFrame extends javax.swing.JFrame {
    
    private Vector<Criterio> criterios;
    private Vector<CriterioExpertoPanel> criteriosPan = new Vector<CriterioExpertoPanel>();
    int nCrit;
    private Plantilla plant;
    private boolean listo;
    private Consultas consultas;
    private int idIP;

    public PlantillaExpertoFrame(Plantilla plant, int idIP, Consultas cons) {
        initComponents();
        this.idIP=idIP;
        this.setVisible(true);
        consultas=cons;
        this.criterios=consultas.getCriteriosPlantilla(plant.getIdPlantilla());
        for(int i=0; i<criterios.size(); i++){
            String st = criterios.get(i).getNombre();
            CriterioExpertoPanel cp = new CriterioExpertoPanel(st);
            System.out.println(idIP);
            System.out.println(criterios.get(i).getNombre());
            System.out.println(consultas.existeCriterioTemporal(idIP)!=0);
            if(consultas.existeCriterioTemporal(idIP)!=0){
                InformeParcial itemp = consultas.getInformeTemporal(idIP, criterios.get(i).getNombre());
                cp.setTexto(itemp.getContenido());
                cp.setNota(itemp.getNota());
            }
            jPanel1.add(cp); 
            criteriosPan.add(cp);
        }
    }
    public PlantillaExpertoFrame() {
        initComponents();
        this.setSize(500, 600);
        for(int i=0; i<3; i++){
            String st = "PRUEBA";
            CriterioExpertoPanel cp = new CriterioExpertoPanel(st);
            jPanel1.add(cp);
            criteriosPan.add(cp);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        CrearPDF = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        Guardar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        NotaTotal = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setLocationByPlatform(true);

        CrearPDF.setText("Crear PDF");
        CrearPDF.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                CrearPDFMouseClicked(evt);
            }
        });

        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.Y_AXIS));
        jScrollPane2.setViewportView(jPanel1);

        Guardar.setText("Guardar");
        Guardar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                GuardarMouseClicked(evt);
            }
        });

        jLabel1.setText("NOTA:");

        NotaTotal.setText("0");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addComponent(NotaTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 118, Short.MAX_VALUE)
                .addComponent(Guardar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(CrearPDF, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(354, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 462, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(CrearPDF)
                        .addComponent(Guardar))
                    .addComponent(NotaTotal, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(482, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addContainerGap()))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CrearPDFMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CrearPDFMouseClicked
        for(int i=0; i<nCrit; i++){
            Criterio ct = new Criterio(plant.getCosultas());
            //ct.setNombre(criteriosPan.get(i).getNombre());
            //ct.setPorcentaje(criteriosPan.get(i).getPorcentaje());
            criterios.add(ct);
            this.dispose();
        }
    }//GEN-LAST:event_CrearPDFMouseClicked

    private void GuardarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_GuardarMouseClicked
        double nota=0;
        for(int i=0; i<criterios.size(); i++){
            nota+=((criterios.get(i).getPorcentaje()/100.0)*criteriosPan.get(i).getNota());
            try {
                if(consultas.existeCriterioTemporal(idIP)<criterios.size()){
                    consultas.añadirCriterioTemporal(idIP, criteriosPan.get(i).getNota(),
                        criteriosPan.get(i).getTexto(), criterios.get(i).getNombre());
                }
                else{
                    consultas.actualizarCriterioTemporal(idIP, criteriosPan.get(i).getNota(),
                        criteriosPan.get(i).getTexto(), criterios.get(i).getNombre());
                }
            } catch (SQLException ex) {
                Logger.getLogger(PlantillaExpertoFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        NotaTotal.setText(nota+"");        
        
    }//GEN-LAST:event_GuardarMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CriteriosFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CriteriosFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CriteriosFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CriteriosFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PlantillaExpertoFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CrearPDF;
    private javax.swing.JButton Guardar;
    private javax.swing.JLabel NotaTotal;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables

}
